/*
 * MonitorLiveDataFrame.java
 *
 * Created on Dec 15, 2011, 11:41:58 PM
 */
package gui;

/**
 *
 * @author suzann
 */

/*
 * Import Java Classes
 */

import java.awt.*;
import java.awt.event.*;
import java.util.List;

/*
 * Import own Classes
 */
import chart.*;


public class MonitorLiveDataFrame extends javax.swing.JFrame {

    /** Creates new form MonitorLiveDataFrame */
    public MonitorLiveDataFrame() {
        initComponents();
       // initLiveDataChart();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jpnStation = new javax.swing.JPanel();
        jcbStation = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(1024, 688));

        jpnStation.setPreferredSize(new java.awt.Dimension(1083, 25));

        jcbStation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07" }));
        jcbStation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbStationActionPerformed(evt);
            }
        });

        jLabel1.setText("Station");

        javax.swing.GroupLayout jpnStationLayout = new javax.swing.GroupLayout(jpnStation);
        jpnStation.setLayout(jpnStationLayout);
        jpnStationLayout.setHorizontalGroup(
            jpnStationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnStationLayout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcbStation, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(739, Short.MAX_VALUE))
        );
        jpnStationLayout.setVerticalGroup(
            jpnStationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnStationLayout.createSequentialGroup()
                .addGroup(jpnStationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbStation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jpnStation, java.awt.BorderLayout.PAGE_END);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-895)/2, (screenSize.height-606)/2, 895, 606);
    }// </editor-fold>                        

    private void jcbStationActionPerformed(java.awt.event.ActionEvent evt) {                                           
        //get the station ids selection, and get the live data of it.
        stationNo = (String)jcbStation.getSelectedItem();
        System.out.println(stationNo);

    	ldcPnl.stopLiveData();
        showLiveDataChart();   
    }                                          

    /**
     * Initialise Chart component
     * @param 
     */
    public void initLiveDataChart()
    {        
        ldcPnl = new LiveDataChartPanel();
        getContentPane().add(ldcPnl, java.awt.BorderLayout.CENTER);
        this.invalidate();
        this.validate();
    }
    
    /**
     * Showing the Live Data Chart
     * @param 
     */
    public void showLiveDataChart()
    {        
        ldcPnl.resetStation(stationNo);
    }
    
    public void startMonitor()
    {
    	initLiveDataChart();
        this.showLiveDataChart();
    }
    
    protected void processWindowEvent(WindowEvent e)
    {
    	if(e.getID() == WindowEvent.WINDOW_CLOSING)
    		onExit();
    }

    private void onExit()
    {
    	ldcPnl.stopLiveData();
    	ldcPnl.setVisible(false);
        this.invalidate();
        this.validate();
                
        //dispose the frame
        this.dispose();
    }

    
    public void addNotify() {
        super.addNotify();
        // Buffer
        createBufferStrategy(2);           
        //BufferStrategy strategy = getBufferStrategy();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MonitorLiveDataFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MonitorLiveDataFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MonitorLiveDataFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MonitorLiveDataFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MonitorLiveDataFrame().setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox jcbStation;
    private javax.swing.JPanel jpnStation;
    // End of variables declaration                   

    // Variables declaration - self
    private LiveDataChartPanel ldcPnl;
    private String stationNo = "01";
    
}
